<?php
require_once($_SERVER['DOCUMENT_ROOT'] . '/all.conf.php');
require_once(__PHY_LIB.'/inc/page.inc.php');
require_once(__PHY_LIB.'/class/uWork.class.php');
require_once(__PHY_LIB.'/class/uWorkSize.class.php');
require_once(__PHY_LIB.'/class/uWorkSizeKuang.class.php');
require_once(__PHY_LIB.'/class/uWorkAlbum.class.php');
require_once(__PHY_LIB.'/class/uWorkSku.class.php');
require_once(__PHY_LIB.'/class/uUser.class.php');
require_once(__PHY_LIB.'/class/uCategory.class.php');

$rP = validate($_GET, 'id:n');
$oHomeWork = new uWork($rP['id']);

/// 图集 ///
$oAlbumList = new uWorkAlbumList();
$oAlbumList->cfgWhere("work_id='".$oHomeWork->getID()."'");
$oAlbumList->cfgOrder("rank");
$oAlbumList->load(5);
while ($oAlbum = $oAlbumList->nextItem()) {
	;
}
$oTpl->assign('rAlbums', $oAlbumList->fetchArray());

/// 尺寸边框，及其SKU ///
if ($oHomeWork->get('has_size')) {
	$oList = new uWorkSizeList();
	$oList->cfgWhere("work_id='".$oHomeWork->getID()."'");
	$oList->cfgOrder("rank");
	$oList->load();
	while ($oSize = $oList->nextItem()) {
		/// 无框数据 ///
		if (0 < $oHomeWork->get('size_id')) {
			$rSku = $oHomeWork->getSku($oSize->getID(), 0);
		}
		else {
			$oHomeWork->set('size_id', $oSize->getID());
			$rSku = $oHomeWork->getSku($oSize->getID(), 0, true);
		}
		$oSize->set('n_price', $rSku['price']);
		$oSize->set('n_price_en', $rSku['price_en']);
		$oSize->set('n_stock', $rSku['stock']);
		$oSize->set('n_stock_max', $rSku['stock_max']);
		/// 有框数据 ///
		$oKuang = new uWorkSizeKuang();
		$oKuang->loadUnique(array(
			'work_id' => $oHomeWork->getID(),
			'size_id' => $oSize->getID(),
		));
		if (0 < $oKuang->getID()) {
			$rSku = $oHomeWork->getSku($oSize->getID(), $oKuang->getID());
			$oSize->set('k_id', $oKuang->getID());
			$oSize->set('k_price', $rSku['price']);
			$oSize->set('k_price_en', $rSku['price_en']);
			$oSize->set('k_stock', $rSku['stock']);
			$oSize->set('k_stock_max', $rSku['stock_max']);
		}
	}
	$oTpl->assign('rSizes', $oList->fetchArray());
}
else {
	$oHomeWork->set('size_id', 0);
	$oHomeWork->getSku(0, 0, true);
}

/// 类别路径 ///
$oCtgry = new uCategory($oHomeWork->get('category_id'));
$oCtgry->set('BreadCrumb', $oCtgry->fetchBreadCrumb());
$oTpl->assign('rCtgry', $oCtgry->getData());

/// 作者 ///
$oUser = new uUser($oHomeWork->get('owner_id'));
$oTpl->assign('rOwner', $oUser->getData());

/// 该用户的其他作品 ///
$oList = new uWorkList();
$oList->cfgWhere("owner_id='".$oHomeWork->get('owner_id')."' AND id!='".$oHomeWork->getID()."'");
$oList->cfgOrder("id DESC");
$oList->load();
while ($oOne = $oList->nextItem()) {
	$oOne->getSku(0, 0, true);
}
$oTpl->assign('rOtherWorks', $oList->fetchArray());


$oTpl->assign('rHomeWork', $oHomeWork->getData());
$oTpl->display(dirname(__FILE__).'/'.basename(__FILE__, '.php').'.tpl');
?>
